package cn.cmcc.online.smsapi.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONObject;

import cn.cmcc.online.smsapi.ui.presenter.VerificationCodePresenter;
import cn.cmcc.online.smsapi.ui.presenter.VerificationCodePresenterImpl;
import cn.cmcc.online.smsapi.ui.util.DensityUtil;
import cn.cmcc.online.smsapi.ui.widget.CompletedWarpTextView;
import cn.cmcc.online.smsapi.ui.widget.SpotDivier;

/**
 * Created by ZhangYifan on 2017/6/21.
 */

public class VerificationCodeVistaImpl extends GeneralBaseVistaImpl implements VerificationCodeVista {
    private VerificationCodePresenter mPresenter;

    private TextView mMainInfo;
    private TextView mHighInfoTitle;
    private CompletedWarpTextView mHighInfoValue;
    private TextView mNoticeInfo;

    private View mDivier;

    public VerificationCodeVistaImpl(Context context) {
        super(context);
    }

    @Override
    protected void init() {
        super.init();
        mPresenter = new VerificationCodePresenterImpl();
        mPresenter.setVista(this);
    }

    @Override
    protected void initLayout() {
        super.initLayout();

        LinearLayout body = new LinearLayout(mContext);
        body.setOrientation(LinearLayout.VERTICAL);
        body.setBackgroundColor(Color.TRANSPARENT);
        body.setPadding(0, 0, 0, DensityUtil.dp2px(mContext, 15));
        body.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dp2px(mContext, 300), -2));
        //----------------------------------------------------------------
        mHighInfoTitle = new TextView(mContext);
        mHighInfoTitle.setTextColor(Color.parseColor("#999999"));
        mHighInfoTitle.setTextSize(12);
        mHighInfoTitle.setGravity(Gravity.CENTER_HORIZONTAL);

        LinearLayout.LayoutParams highLightTitleParams = new LinearLayout.LayoutParams(-1, -2);
        highLightTitleParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 30), DensityUtil.dp2px(mContext, 15), 0);
        body.addView(mHighInfoTitle, highLightTitleParams);

        mHighInfoValue = new CompletedWarpTextView(mContext);
        mHighInfoValue.setTextSize(34);
        mHighInfoValue.setTextAlignCenter();
        LinearLayout.LayoutParams highLightContentParams = new LinearLayout.LayoutParams(-1, -2);
        highLightContentParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 10), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15));
        body.addView(mHighInfoValue, highLightContentParams);
        //----------------------------------------------------------------
        mMainInfo = new TextView(mContext);
        mMainInfo.setTextColor(Color.parseColor("#999999"));
        mMainInfo.setTextSize(12);

        LinearLayout.LayoutParams midParams = new LinearLayout.LayoutParams(-1, -2);
        midParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), 0);
        body.addView(mMainInfo, midParams);
        //----------------------------------------------------------------
        mDivier = new SpotDivier(mContext);
        LinearLayout.LayoutParams divierParams = new LinearLayout.LayoutParams(-1, DensityUtil.dp2px(mContext, 2));
        divierParams.setMargins(0, DensityUtil.dp2px(mContext, 15), 0, 0);
        body.addView(mDivier, divierParams);
        //----------------------------------------------------------------
        mNoticeInfo = new TextView(mContext);
        mNoticeInfo.setTextSize(12);
        mNoticeInfo.setTextColor(Color.parseColor("#666666"));
        LinearLayout.LayoutParams lessImportContentParams = new LinearLayout.LayoutParams(-1, -2);
        lessImportContentParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), 0);
        body.addView(mNoticeInfo, lessImportContentParams);

        super.addBody(body);
    }

    @Override
    public void bind(Context context, JSONObject data) {
        super.bind(context, data);
        mPresenter.bind(data);
    }

    @Override
    public void initDefaultView() {
        super.initDefaultView();
    }

    @Override
    public void setHighInfoTtile(String title) {
        mHighInfoTitle.setText(title);
    }

    @Override
    public void setHighInfoValue(String value) {
        mHighInfoValue.setText(value);
    }

    @Override
    public void setHighTitleVisible(int visible) {
        mHighInfoTitle.setVisibility(visible);
    }

    @Override
    public void setHighInfoColor(int color) {
        mHighInfoValue.setTextColor(color);
    }

    @Override
    public void setHighInfoTextSize(int sp) {
        mHighInfoValue.setTextSize(sp);
    }

    @Override
    public void setMainInfo(String mainInfo) {
        mMainInfo.setText(mainInfo);
    }

    @Override
    public void setMainInfoVisible(int visible) {
        mMainInfo.setVisibility(visible);
    }

    @Override
    public void setTip(String tip) {
        mNoticeInfo.setText(tip);
    }

    @Override
    public int getSingleLineTextSize(String content) {
        int size = 34 + 1;
        float mesureWid = 0;
        int maxWid = DensityUtil.dp2px(mContext, 270);
        Paint paint = new Paint();

        do {
            paint.setTextSize(DensityUtil.sp2px(mContext, --size));
            mesureWid = paint.measureText(content);
        }
        while (mesureWid > maxWid);

        return size;
    }
}
